import requests
import os
def GetCodeList():
    try:
        os.mkdir('./cache')
    except Exception as e:
        pass
    try:
        os.mkdir('./cache/{0}'.format(ClassFicationCode))
    except Exception as e:
        pass
    try:
        f=open('./conf/reporterAreas.json')
        f.close()
    except Exception:
        content = requests.get("https://comtrade.un.org/Data/cache/reporterAreas.json")
        with open('./conf/reporterAreas.json','wb') as fp:
            fp.write(content.content)
    try:
        f=open('./conf/classificationH4.json')
        f.close()
    except Exception:
        content = requests.get("https://comtrade.un.org/Data/cache/classificationH4.json")
        with open('./conf/classificationH4.json','wb') as fp:
            fp.write(content.content)

import json
from conf.config import ClassFicationCode,start,end
def DownloadList():
    try:
        f=open('./cache/{0}/cache.json'.format(ClassFicationCode))
        f.close()
        print('CacheList existed')
    except IOError:
        with open('./conf/reporterAreas.json','r',encoding='utf-8-sig') as f:
            RawDig=json.load(fp=f)
        RDict=dict()
        DropList=list()
        DropList.append('all')
        if start > 1963:
            DropList.append('457')
            DropList.append('461')
            if start > 1964:
                DropList.append('835')
                DropList.append('836')
                if start >1972:
                    DropList.append('647')
                    if start > 1974:
                        DropList.append('866')
                        DropList.append('868')
                        DropList.append('230')
                        if start > 1980:
                            DropList.append('717')
                            if start > 1981:
                                DropList.append('841')
                                if start > 1989:
                                    DropList.append('886')
                                    DropList.append('720')
                                    DropList.append('278')
                                    DropList.append('280')
                                    if start > 1991:
                                        DropList.append('810')
                                        DropList.append('890')
                                        if start > 1993:
                                            DropList.append('582')
                                            if start > 1998:
                                                DropList.append('590')
                                                DropList.append('592')
                                                if start > 2011:
                                                    DropList.append('736')
        Period=list()
        for i in range(start,end+1):
            Period.append(str(i))
        for i in RawDig.get('results'):
            if i.get('id') in DropList:
                continue
            tdata={i.get('text'):Period}
            data={i.get('id'):tdata}
            RDict.update(data)
        with open('./cache/{0}/cache.json'.format(ClassFicationCode),'w') as f:
            f.write(json.dumps(RDict,indent=4))
        with open('./cache/{0}/cache1.json'.format(ClassFicationCode),'w') as f:
            f.write(json.dumps(RDict,indent=4))
    with open('./cache/{0}/cache.json'.format(ClassFicationCode),'r') as f:
        RDict=json.load(fp=f)